package com.wangyg.juc.ProducerConsumer.version3;

import java.util.List;

/**
 * Created by wangyg6 on 2018/7/11.
 */
public class Consumer implements Runnable {
	private List<String> queue;

	public Consumer setQueue(List<String> queue) {
		this.queue = queue;
		return this;
	}

	@Override
	public void run() {
		while (true) {
			Main.lock.lock();
			if (queue.size() <= 0) {
				try {
					Main.full.signalAll();
					Main.empty.await();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}

			if (queue.size() > 0) {
				String remove = queue.remove(0);
				System.out.println(Thread.currentThread().getName() + "消费了" + remove);
			}
			Main.lock.unlock();
			try {
				Thread.sleep(10);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}

		}
	}
}
